//    LunchIT - Tool to make decisions of a lunch restaurant.
//    Copyright (C) 2010 Jukka Hell
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package fi.hell.util

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpSession
import javax.servlet.http.Cookie
import org.springframework.web.context.request.RequestContextHolder
import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH
import org.apache.commons.codec.binary.Base64

/**
 *
 */
class CookieService {

    boolean transactional = false

    static String getCookie(String name) {
        HttpServletRequest request = RequestContextHolder.currentRequestAttributes().request
        Cookie cke = request.cookies.find {it.name == name}

        if (cke) {
            return cke.value
        }

        return null
    }

    static void setCookie(String name, String value, HttpServletResponse response) {
        int defaultMaxAge = -1
        setCookie(name, value, defaultMaxAge, response)
    }

    static void setCookie(String name, String value, int maxAge, HttpServletResponse response) {
        Cookie cookie = new Cookie(name, value)
        cookie.domain = CH.config.utils.cookie.domain // ".symmetria.fi"
        cookie.path = CH.config.utils.cookie.path // "/"
        cookie.maxAge = maxAge
        cookie.secure = CH.config.utils.cookie.secure
        response.addCookie(cookie)
    }

    String getSessionId() {
        return getSession().id
    }

    void deleteCookie(String name, HttpServletResponse response, String domain = null) {
        if (! domain) {
            domain = CH.config.utils.cookie.domain
        }

        Cookie cookie = new Cookie(name, null)

        if (domain) {
            cookie.setDomain(domain)
        }

        cookie.setMaxAge(0)
        cookie.setPath("/")
        response.addCookie(cookie)
    }

    public String encodeTokenCookie(String[] cookieTokens) {
        StringBuilder sb = new StringBuilder()

        for (int i=0; i < cookieTokens.length; i++) {
            sb.append(cookieTokens[i])

            if (i < cookieTokens.length - 1) {
                sb.append(":")
            }
        }

        String value = sb.toString()
        sb = new StringBuilder(new String(Base64.encodeBase64(value.getBytes())))

        while (sb.charAt(sb.length() - 1) == '=') {
            sb.deleteCharAt(sb.length() - 1)
        }

        return sb.toString()
    }

    static HttpSession getSession() {
        return RequestContextHolder.currentRequestAttributes().getSession()
    }
}
